package cn.itcast.foodapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import javax.security.auth.login.LoginException;

import cn.itcast.foodapp2.bean.FindOneBean;
import cn.itcast.foodapp2.net.NetUtil;

public class FoodDetailActivity extends AppCompatActivity {
    private static final String TAG = "FoodDetailActivity";
    int id = 0;
    TextView mTvFoodName;
    ImageView mIvFoodPic;
    TextView mTvFoodPrice;
    Button mBtnComment;
    Button mBtnListComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);
        id = getIntent().getIntExtra("foodId", 0);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        mBtnListComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodDetailActivity.this, CommentsListActivity.class);
                startActivity(intent);
            }
        });
        mBtnComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodDetailActivity.this, CommentsAddActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("id", id);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String url = "http://42.48.39.55:8081/foodserver/food/findone";
                String data = NetUtil.getData(jsonObject, url);
                Gson gson = new Gson();
                final FindOneBean findOneBean = gson.fromJson(data, FindOneBean.class);
                Log.e(TAG, "run:" + findOneBean);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvFoodPrice.setText(findOneBean.getData().getPrice().toString() + "$");
                        mTvFoodName.setText(findOneBean.getData().getName());
                        Picasso.with(FoodDetailActivity.this)
                                .load("http://42.48.39.55:8081/foodserver/" + findOneBean.getData().getIcon())
                                .into(mIvFoodPic);
                    }
                });
            }
        }).start();
    }

    private void initView() {
        mTvFoodName = findViewById(R.id.tv_food_name);
        mIvFoodPic = findViewById(R.id.iv_food_pic);
        mTvFoodPrice = findViewById(R.id.tv_food_price);
        mBtnComment = findViewById(R.id.btn_add_comments);
        mBtnListComment = findViewById(R.id.btn_list_comments);
    }
}